module.exports = function ($scope, $resource, toaster, FileUploader) {
    $scope.url = '/api/as/mc/shopcommon/save'
    $scope.bean = {
        id: '',
        name: '',
        share_describe: '',
        bottom_describe: '',
        default_class_logo: '',
        top_pics: [
            {img: '', url: ''},
            {img: '', url: ''},
            {img: '', url: ''},
            {img: '', url: ''}
        ],
        background_pic: '',
        default_logo: '',
        gzh_history_url:'',
        theme: '',
        qr_code: '',
        search_keyword:'',
        search : '',
        switch_page : ''
    };

    $scope.search_list = [];
    $resource('/api/as/mc/shopcommon/info', {}, {}).save({}, function (res) {
        if (res.errcode == 10003) {
            toaster.info({ title: '', body: '您未设置商城信息' });
        } else if (res.errcode === 0) {
            if (res.data) {
                $scope.url = '/api/as/mc/shopcommon/update';
                $scope.bean = res.data;
                if($scope.bean.search){
                    var arr= $scope.bean.search.split(',');
                    for(var i = 0; i < arr.length ; i++){
                        $scope.search_list.push({'search': arr[i]});
                    }
                }
                if ($scope.bean.top_pics) {
                    $scope.bean.top_pics = eval(`(${$scope.bean.top_pics})`)
                } else {
                    $scope.bean.top_pics = [
                        {img: '', url: '' },
                        {img: '', url: '' },
                        {img: '', url: '' },
                        {img: '', url: '' }
                    ]
                }
            }
        } else {
            toaster.error({ title: '', body: res.errmsg });
        }
    });

    //添加
    $scope.ok = function () {
        if($scope.bean.mobile.length > 11){
            toaster.warning({ title: '', body: "电话位数请勿超过11位！"});
            return;
        }
        if($scope.bean.profit_percent && $scope.bean.profit_percent >= 0 && $scope.bean.profit_percent <= 100){
        } else {
            toaster.warning({ title: '', body: "商户让利错误，请填写0-100之间数值"});
            return;
        }
        if($scope.bean.profit_level1 + $scope.bean.profit_level2 + $scope.bean.profit_level3 != 100){
            toaster.warning({ title: '', body: "三级分润总和应为100%"});
            return;
        }
        for(var i in $scope.bean.top_pics){
            if($scope.bean.top_pics[i].url == '' || $scope.bean.top_pics[i].url == undefined){
                $scope.bean.top_pics[i].url = '#';
            }
        }
        
        var str = '';
        for(var i in $scope.search_list){
            str = str + $scope.search_list[i].search + ',';
        }
        str = str.substring(0,str.length - 1);
        $scope.bean.search = str;

        var para = {
            id: $scope.bean.id,
            name: $scope.bean.name,
            share_describe: $scope.bean.share_describe,
            bottom_describe: $scope.bean.bottom_describe,
            default_class_logo: $scope.bean.default_class_logo,
            top_pics: JSON.stringify($scope.bean.top_pics),
            background_pic: $scope.bean.background_pic,
            default_logo: $scope.bean.default_logo,
            theme: $scope.bean.theme,
            qr_code: $scope.bean.qr_code,
            wxtip_pic: $scope.bean.wxtip_pic,
            search_keyword : $scope.bean.search_keyword,
            search : $scope.bean.search,
            mobile : $scope.bean.mobile,
            gzh_history_url : $scope.bean.gzh_history_url,
            service_time : $scope.bean.service_time,
            switch_page : $scope.bean.switch_page,
            profit_flag: $scope.bean.profit_flag,
            profit_percent: $scope.bean.profit_percent,
            profit_level1: $scope.bean.profit_level1,
            profit_level2: $scope.bean.profit_level2,
            profit_level3: $scope.bean.profit_level3
        }
        $resource($scope.url, {}, {}).save(para, function (res) {
            if (res.errcode === 0) {
                toaster.success({ title: '', body: '操作成功' });
            } else {
                toaster.error({ title: '', body: res.errmsg });
            }
        });
    }

    //default_class_logo
    $scope.uploader_default_class_logo = new FileUploader({
        url: 'https://txy.juyouhx.com/Api/Api/ObjectToOss?topdir=skv4&selfdir=common'+ Date.parse(new Date())
    });
    $scope.uploader_default_class_logo.filters.push({
        name: 'imageFilter',
        fn: function (item /*{File|FileLikeObject}*/, options) {
            var type = '|' + item.type.slice(item.type.lastIndexOf('/') + 1) + '|';
            return '|jpg|png|jpeg|bmp|gif|'.indexOf(type) !== -1;
        }
    });
    $scope.uploader_default_class_logo.onSuccessItem = function (fileItem, response, status, headers) {
        $scope.bean.default_class_logo = response.savename;
    };

    //background_pic
    $scope.uploader_background_pic = new FileUploader({
        url: 'https://txy.juyouhx.com/Api/Api/ObjectToOss?topdir=skv4&selfdir=common'+ Date.parse(new Date())
    });
    $scope.uploader_background_pic.filters.push({
        name: 'imageFilter',
        fn: function (item /*{File|FileLikeObject}*/, options) {
            var type = '|' + item.type.slice(item.type.lastIndexOf('/') + 1) + '|';
            return '|jpg|png|jpeg|bmp|gif|'.indexOf(type) !== -1;
        }
    });
    $scope.uploader_background_pic.onSuccessItem = function (fileItem, response, status, headers) {
        $scope.bean.background_pic = response.savename;
    };

    //default_logo
    $scope.uploader_default_logo = new FileUploader({
        url: 'https://txy.juyouhx.com/Api/Api/ObjectToOss?topdir=skv4&selfdir=common'+ Date.parse(new Date())
    });
    $scope.uploader_default_logo.filters.push({
        name: 'imageFilter',
        fn: function (item /*{File|FileLikeObject}*/, options) {
            var type = '|' + item.type.slice(item.type.lastIndexOf('/') + 1) + '|';
            return '|jpg|png|jpeg|bmp|gif|'.indexOf(type) !== -1;
        }
    });
    $scope.uploader_default_logo.onSuccessItem = function (fileItem, response, status, headers) {
        $scope.bean.default_logo = response.savename;
    };

    //qr_code
    $scope.uploader_qr_code = new FileUploader({
        url: 'https://txy.juyouhx.com/Api/Api/ObjectToOss?topdir=skv4&selfdir=common'+ Date.parse(new Date())
    });
    $scope.uploader_qr_code.filters.push({
        name: 'imageFilter',
        fn: function (item /*{File|FileLikeObject}*/, options) {
            var type = '|' + item.type.slice(item.type.lastIndexOf('/') + 1) + '|';
            return '|jpg|png|jpeg|bmp|gif|'.indexOf(type) !== -1;
        }
    });
    $scope.uploader_qr_code.onSuccessItem = function (fileItem, response, status, headers) {
        $scope.bean.qr_code = response.savename;
    };

    //wxtip_pic
    $scope.uploader_wxtip_pic = new FileUploader({
        url: 'https://txy.juyouhx.com/Api/Api/ObjectToOss?topdir=skv4&selfdir=common'+ Date.parse(new Date())
    });
    $scope.uploader_wxtip_pic.filters.push({
        name: 'imageFilter',
        fn: function (item /*{File|FileLikeObject}*/, options) {
            var type = '|' + item.type.slice(item.type.lastIndexOf('/') + 1) + '|';
            return '|jpg|png|jpeg|bmp|gif|'.indexOf(type) !== -1;
        }
    });
    $scope.uploader_wxtip_pic.onSuccessItem = function (fileItem, response, status, headers) {
        $scope.bean.wxtip_pic = response.savename;
    };
    




    //top_pics_1
    $scope.uploader_top_pics_1 = new FileUploader({
        url: 'https://txy.juyouhx.com/Api/Api/ObjectToOss?topdir=skv4&selfdir=common'+ Date.parse(new Date())
    });
    $scope.uploader_top_pics_1.filters.push({
        name: 'imageFilter',
        fn: function (item /*{File|FileLikeObject}*/, options) {
            var type = '|' + item.type.slice(item.type.lastIndexOf('/') + 1) + '|';
            return '|jpg|png|jpeg|bmp|gif|'.indexOf(type) !== -1;
        }
    });
    $scope.uploader_top_pics_1.onSuccessItem = function (fileItem, response, status, headers) {
        $scope.bean.top_pics[0].img = response.savename;
    };

    //top_pics_2
    $scope.uploader_top_pics_2 = new FileUploader({
        url: 'https://txy.juyouhx.com/Api/Api/ObjectToOss?topdir=skv4&selfdir=common'+ Date.parse(new Date())
    });
    $scope.uploader_top_pics_2.filters.push({
        name: 'imageFilter',
        fn: function (item /*{File|FileLikeObject}*/, options) {
            var type = '|' + item.type.slice(item.type.lastIndexOf('/') + 1) + '|';
            return '|jpg|png|jpeg|bmp|gif|'.indexOf(type) !== -1;
        }
    });
    $scope.uploader_top_pics_2.onSuccessItem = function (fileItem, response, status, headers) {
        $scope.bean.top_pics[1].img = response.savename;
    };

    //top_pics_3
    $scope.uploader_top_pics_3 = new FileUploader({
        url: 'https://txy.juyouhx.com/Api/Api/ObjectToOss?topdir=skv4&selfdir=common'+ Date.parse(new Date())
    });
    $scope.uploader_top_pics_3.filters.push({
        name: 'imageFilter',
        fn: function (item /*{File|FileLikeObject}*/, options) {
            var type = '|' + item.type.slice(item.type.lastIndexOf('/') + 1) + '|';
            return '|jpg|png|jpeg|bmp|gif|'.indexOf(type) !== -1;
        }
    });
    $scope.uploader_top_pics_3.onSuccessItem = function (fileItem, response, status, headers) {
        $scope.bean.top_pics[2].img = response.savename;
    };

    //top_pics_4
    $scope.uploader_top_pics_4 = new FileUploader({
        url: 'https://txy.juyouhx.com/Api/Api/ObjectToOss?topdir=skv4&selfdir=common'+ Date.parse(new Date())
    });
    $scope.uploader_top_pics_4.filters.push({
        name: 'imageFilter',
        fn: function (item /*{File|FileLikeObject}*/, options) {
            var type = '|' + item.type.slice(item.type.lastIndexOf('/') + 1) + '|';
            return '|jpg|png|jpeg|bmp|gif|'.indexOf(type) !== -1;
        }
    });
    $scope.uploader_top_pics_4.onSuccessItem = function (fileItem, response, status, headers) {
        $scope.bean.top_pics[3].img = response.savename;
        console.log($scope.bean)
        console.log(response.savename)
        console.log($scope.bean.top_pics[3].img)
    };

        //删除图片
        $scope.delimg = function (num) {
            for(var i = 0 ; i < 4 ; i++ ){
                if(i + 1 == num){
                    $scope.bean.top_pics[i] = {
                        img: '#',
                        url: ''
                    };
                }
            }
            console.log($scope.bean.top_pics)
        }

        //添加热搜
        $scope.add = function () {
            // console.log($scope.search);
            for(var i = 0; i < $scope.search_list.length; i++){
                if($scope.search == $scope.search_list[i].search){
                    toaster.error({title: '', body: '不能重复添加'});
                    return;
                }
            }
            if ($scope.search != '' && $scope.search != undefined) {
                var temp = {
                    'search': $scope.search
                };
                $scope.search_list.push(temp);
            }
            
            $scope.search = '';
            
        }
    
        //删除已添加热搜
        $scope.remove = function (index) {
            $scope.search_list.splice(index,1)
        };

        // $scope.descimg = function() {
        //   $('.showok').hover(function(){
        //     console.log(this);
        //     this.children('img').attr('display','block');
        //   });
        // }
        // $scope.descimg();
};